package net.whatmark.playavillain.meshikau.page;

import javax.servlet.http.HttpServletRequest;

import org.t2framework.t2.annotation.composite.POST;
import org.t2framework.t2.annotation.core.ActionParam;
import org.t2framework.t2.annotation.core.Default;
import org.t2framework.t2.annotation.core.Page;
import org.t2framework.t2.annotation.core.Upload;
import org.t2framework.t2.contexts.UploadFile;
import org.t2framework.t2.navigation.Forward;
import org.t2framework.t2.spi.Navigation;

@Page("upload")
public class UploadPage {

	@Default
	public Navigation index() {
		return Forward.to("/pages/upload.jsp");
	}

	@POST
	@ActionParam
	public Navigation upload(UploadFile file, HttpServletRequest request) {
		System.out.println("file:" + file.getName());
		System.out.println("size:" + file.getSize());
		System.out.println("contentType:" + file.getContentType());
		request.setAttribute("fileUploadResult", file.getName()
				+ " is uploaded.");
		return Forward.to("/pages/upload.jsp");
	}

	@POST
	@ActionParam
	public Navigation upload2(UploadFile[] files, HttpServletRequest request) {
		StringBuilder builder = new StringBuilder();
		for (UploadFile file : files) {
			System.out.println("file:" + file.getName());
			System.out.println("size:" + file.getSize());
			System.out.println("contentType:" + file.getContentType());
			builder.append(file.getName());
			builder.append(", ");
		}
		builder.setLength(builder.length() - 2);
		request.setAttribute("fileUploadResult", builder.toString()
				+ " is uploaded.");
		return Forward.to("/pages/upload.jsp");
	}

	/**
	 * {@.en  }
	 *
	 * <br />
	 *
	 * {@.ja ファイルがアップロードされたときに、明示的にどのファイルかを指定する場合は、 {@code @Upload}
	 * でどのファイルかを指定します.}
	 *
	 * @param file
	 * @param request
	 * @return
	 */
	@POST
	@ActionParam
	public Navigation upload3(@Upload("bbb") UploadFile file,
			HttpServletRequest request) {
		System.out.println("upload3");
		System.out.println("file:" + file.getName());
		System.out.println("size:" + file.getSize());
		System.out.println("contentType:" + file.getContentType());
		request.setAttribute("fileUploadResult", file.getName()
				+ " is uploaded.");
		return Forward.to("/pages/upload.jsp");
	}

}
